﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace McgsHttp
{
    public partial class Form1 : Form
    {
        public delegate void setLabelTextDelegate(string  str);
        public delegate string getLabelTextDelegate(string id);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            httpServer server = new httpServer();
            server.http_get += Server_http_get;
            server.http_post += Server_http_post;
            server.HttpStart();

         }
 
        private void Server_http_post(string devid, string state, string datetime)
        {
            set_label_id(devid);
            set_label_state(state);
            set_label_time(datetime);
        }

        private string Server_http_get(string devid)
        {
            return get_text_arg(devid);
        }
        private void set_label_id(string str)
        {
            if (this.InvokeRequired)
            {
                setLabelTextDelegate st = new setLabelTextDelegate(set_label_id);
                this.Invoke(st, str);
            }
            else
            {
                this.label_devid.Text = str;
            }
        }
        private void set_label_state(string str)
        {
            if (this.InvokeRequired)
            {
                setLabelTextDelegate st = new setLabelTextDelegate(set_label_state);
                this.Invoke(st, str);
            }
            else
            {
                this.label_status.Text = str;
            }
        }
        private void set_label_time(string str)
        {
            if (this.InvokeRequired)
            {
                setLabelTextDelegate st = new setLabelTextDelegate(set_label_time);
                this.Invoke(st, str);
            }
            else
            {
                this.label_dtime.Text = str;
            }
        }
       private string get_text_arg(string id)
        {
            if(this.InvokeRequired)
            {
                getLabelTextDelegate gt = new getLabelTextDelegate(get_text_arg); 
                return (string)this.Invoke(gt, id);
            }
            else
            {
                return this.textBox_arg.Text;
            }
        }
    }
}
